const {
  baseUrl,
  getRequest,
  postRequest
} = require("../../../utils/request.js")
const {
  mock_flag,
  login_flag
} = require("../../../data/index.js")
const {
  repairReportList3_mock
} = require("../../../mock/repairReport.js")

Page({
  data: {
    // 用户id
    userId: 0,
    // 搜索条件
    searchInfo: "",
    // 当前页
    currentPage: 1,
    // 每页大小
    defaultPageSize: 4,
    // 数据总条数
    totalData: 0,
    // 维修报告列表
    repairReportList: []
  },
  onLoad(options) {
    // 登录拦截
    const isLogin = wx.getStorageSync('isLogin');
    if (!isLogin) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      if (login_flag) {
        wx.redirectTo({
          url: '/pages/user/wxlogin/wxlogin'
        });
      }
      if (!login_flag) {
        wx.redirectTo({
          url: '/pages/user/login/login'
        });
      }
    }
    // 初始化数据
    this.setData({
      userId: wx.getStorageSync('userId'), // 获取用户id
    })
    // mock数据
    if (mock_flag) {
      this.setData({
        repairReportList: repairReportList3_mock
      })
    }
    // 网络请求
    if (!mock_flag) {
      this.http_repairReport_searchByStatus()
    }
  },
  //  接取维修函数
  acceptRepair: function (e) {
    //console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/work/task_add/task_add?id='+e.currentTarget.dataset.id,
    }) //携带参数跳转
  },
   /**
   * 上拉刷新事件
   */
  onReachBottom() {
    console.log("上拉刷新")
    // 老数据合并新数据做累加操作
    if (mock_flag) {
      this.setData({
        repairReportList: this.data.repairReportList.concat(repairReportList3_mock)
      })
    }
    if (!mock_flag) {
      this.setData({
        currentPage: this.data.currentPage + 1
      })
      this.http_repairReport_searchByStatus()
    }
  },
   /**
   * 网络请求
   */
  // 分页查询,状态为待处理
  http_repairReport_searchByStatus() {
    getRequest(baseUrl + "/front/repairReport/searchByStatus", {
      search: this.data.searchInfo,
      page: this.data.currentPage,
      size: this.data.defaultPageSize,
    }).then(res => {
      // console.log(res.data)
      if (res.code == 200) {
        this.setData({
          repairReportList: this.data.repairReportList.concat(res.data.records)
        })
      }
    })
  },
})

    